﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyTruongHoc_App.MyDataSet;
using QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters;

namespace QuanLyTruongHoc_App
{
    public partial class QLHocSinh : Form
    {
        public QLHocSinh()
        {
            InitializeComponent();
            dg_DanhSach.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dg_DanhSach_DataBindingComplete);
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.Format = "dd/MM/yyyy";
            grc_NgayNhapTruong.DefaultCellStyle = 
                grc_NgaySinh.DefaultCellStyle = style;
            
        }

        private void QuanLyGiaoVien_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void refreshData()
        {
            this.view_HocSinhTableAdapter.Connection.ConnectionString =
                PublicLib.getStrConn();
            this.view_HocSinhTableAdapter.FillByFlag(this.qLTH.view_HocSinh, PConst.FLAG_NONE);
            reloadData();

            MyDataSet.QLTHTableAdapters.tbl_LopHocTableAdapter lopAdap = new tbl_LopHocTableAdapter();
            lopAdap.Connection.ConnectionString = PublicLib.getStrConn();
            lopAdap.Fill(qLTH.tbl_LopHoc);

            tscb_Lop.Items.Clear();
            for (int i = 0; i < qLTH.tbl_LopHoc.Rows.Count; i++)
            {
                tscb_Lop.Items.Add(qLTH.tbl_LopHoc.Rows[i][qLTH.tbl_LopHoc.TenLopColumn].ToString());
            }

            tscb_Lop.Items.Add(MSG.MNU_ALL);
        }

        private void QuanLyGiaoVien_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 20;
            dg_DanhSach.Height = stt_Bottom.Top - dg_DanhSach.Top - 1;

            //pn_TimKiem.Top = 
            //pn_Button.Top = dg_DanhSach.Bottom + 5;
            //pn_Button.Left = dg_DanhSach.Right - pn_Button.Width;
        }

        private void dg_DanhSach_DataBindingComplete(object sender, EventArgs e)
        {
            reloadData();
        }

        private void reloadData()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[cl_MaGV.Name].Value = PublicLib.formatHSId(
                    int.Parse(dg_DanhSach.Rows[i].Cells[grc_id.Name].Value.ToString()));

                String tmp = "Nam";
                if (((int)dg_DanhSach.Rows[i].Cells[grc_GioiTinh_DB.Name].Value) == 1)
                    tmp = "Nữ";

                dg_DanhSach.Rows[i].Cells[grc_GioiTinh_Disp.Name].Value = tmp;
            }
        }

        private void dg_DanhSach_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == cl_MaGV.Index)
            {
                switch (cl_MaGV.HeaderCell.SortGlyphDirection)
                {
                    case SortOrder.None:
                    case SortOrder.Descending:
                        dg_DanhSach.Sort(grc_id, ListSortDirection.Ascending);
                        cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                        break;

                    case SortOrder.Ascending:
                        dg_DanhSach.Sort(grc_id, ListSortDirection.Descending);
                        cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.Descending;
                        break;
                }
            }
            else
            {
                cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.None;
                if (e.ColumnIndex == grc_GioiTinh_Disp.Index)
                {
                    switch (grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection)
                    {
                        case SortOrder.None:
                        case SortOrder.Descending:
                            dg_DanhSach.Sort(grc_GioiTinh_DB, ListSortDirection.Ascending);
                            grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                            break;

                        case SortOrder.Ascending:
                            dg_DanhSach.Sort(grc_GioiTinh_DB, ListSortDirection.Descending);
                            grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection = SortOrder.Descending;
                            break;
                    }
                }
                else
                    grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection = SortOrder.None;
            }
        }

        private void frm_ThemHS_Disposed(object sender, EventArgs e)
        {
            refreshData();
        }

        private void dg_DanhSach_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            tsb_Edit_Click(tsb_Edit, new EventArgs());
        }

        private void deleteGV()
        {
            //String idHS = dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString();
            //DataRow hs = qLTH.view_HocSinh.Select(qLTH.view_HocSinh.idColumn.ColumnName + "=" + idHS)[0];
            //hs[qLTH.view_HocSinh.FlagColumn] = PConst.FLAG_DELETE;

            //view_HocSinhTableAdapter.Update(hs);
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            deleteGV();
        }

        private void tsb_TaoMoi_Click(object sender, EventArgs e)
        {
            AddEditHocSinh frm_ThemHS = new AddEditHocSinh();
            frm_ThemHS.Show();
            frm_ThemHS.Disposed += new EventHandler(frm_ThemHS_Disposed);
        }

        private void tsb_Edit_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            AddEditHocSinh frm_EditHS = new AddEditHocSinh();
            frm_EditHS.Show();
            frm_EditHS.setEditInfo(int.Parse(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString()));
            frm_EditHS.Disposed += new EventHandler(frm_ThemHS_Disposed);
        }

        private void tsb_Delete_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;

            deleteGV();
            refreshData();
        }

        private void tscb_Lop_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Data.DataView dv = qLTH.view_HocSinh.DefaultView;
            
            if (tscb_Lop.SelectedIndex == -1 || tscb_Lop.SelectedIndex == tscb_Lop.Items.Count - 1)
            {
                dv.RowFilter = "";
            }
            else
            {
                dv.RowFilter = "(" + qLTH.view_HocSinh.idLopColumn + "=" + 
                    qLTH.tbl_LopHoc.Rows[tscb_Lop.SelectedIndex][qLTH.tbl_LopHoc.idColumn].ToString() + ")";
            }

            dg_DanhSach.DataSource = dv.ToTable();
        }
    }
}
